#ifdef a
#include"Quene.h"

//节点创建
QNode* BuyQNode(TYPEQ val) {
	QNode* new_qnode = (QNode*)malloc(sizeof(QNode));
	if (!new_qnode) {
		assert(0);
		return NULL;
	}
	new_qnode->next = NULL;
	new_qnode->val = val;
	return new_qnode;
}

//队列初始化
void QueneInit(Quene* q) {
	assert(q);
	QNode* head = BuyQNode(0);
	q->front = head;
	q->fear = head;
	return;
}

//入队
void QuenePush(Quene* q, TYPEQ val) {
	assert(q);
	QNode* new_qnode = BuyQNode(val);
	q->fear->next = new_qnode;
	q->fear = new_qnode;
	return;
}

//出队
void QueneOut(Quene* q) {
	if (QueneIsEmpty(q))
		return;
	QNode* del_qnode = q->front->next;
	q->front->next = del_qnode->next;
	free(del_qnode);
	del_qnode = NULL;
	return;
}

//队列元素个数
int QueneSize(Quene* q) {
	assert(q);
	int count = 0;
	QNode* qcur = q->front->next;
	while (qcur) {
		count++;
		qcur = qcur->next;
	}
	return count;
}

//查空
int QueneIsEmpty(Quene* q) {
	assert(q);
	return q->front == q->fear;
}

//队头
TYPEQ QueneFront(Quene* q) {
	if (QueneIsEmpty(q))
		return -1111;//代表空
	return q->front->next->val;
}

//队尾
TYPEQ QueneFear(Quene* q){
	if (QueneIsEmpty(q))
		return -1111;//代表空
	return q->fear->val;
}

//销毁
void QueneDestory(Quene* q) {
	assert(q);
	QNode* del_qnode = NULL;
	while (q->front) {
		del_qnode = q->front;
		q->front = del_qnode->next;
		free(del_qnode);
		del_qnode = NULL;
	}
	q->front = q->fear = NULL;
	return;
}


//打印
void QuenePrint(Quene* q) {
	assert(q);
	QNode* qcur = q->front->next;
	while (qcur){
		printf("%d", qcur->val);
		if (qcur != q->fear)
			printf("-->");
		qcur = qcur->next;
	}
	printf("\n");
	return;
}
//--------------------------------------------------------------
void TestQuene() {
	Quene q;
	QueneInit(&q);
	QuenePush(&q, 1);
	QuenePush(&q, 2);
	QuenePush(&q, 3);
	QuenePush(&q, 4);
	QuenePrint(&q);
	printf("队头是: %d\n", QueneFront(&q));
	printf("队尾是: %d\n", QueneFear(&q));
	printf("元素个数为: %d\n", QueneSize(&q));

	printf("\n\n");
	QueneOut(&q);
	QueneOut(&q);
	QuenePrint(&q);
	printf("队头是: %d\n", QueneFront(&q));
	printf("队尾是: %d\n", QueneFear(&q));
	printf("元素个数为: %d\n", QueneSize(&q));
	
	QueneDestory(&q);
	
	return;
}
#endif 